/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.ktmall.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.ktmall.entity.AdminUser;
import com.ktjiaoyu.ktmall.service.AdminUserService;
import com.ktjiaoyu.ktmall.service.impl.AdminUserServiceImpl;
import com.ktjiaoyu.ktmall.utils.Page;
import com.ktjiaoyu.ktmall.utils.R;
import com.ktjiaoyu.ktmall.utils.RequestUtil;
import com.ktjiaoyu.ktmall.utils.SysConstants;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * AdminUser业务控制器
 * @author 科泰集团教研团队
 * @createTime 2024-4-9 17:36:33
 * @lastModify 2024-4-9 17:36:33
 * @version 1.0.0
 */
@WebServlet(name = "AdminUserServlet", urlPatterns = "/api/AdminUser")
@MultipartConfig(maxFileSize = 1024 * 1024 * 50) // 最多传50M
public class AdminUserServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String opr = request.getParameter("opr");
        AdminUserService adminUserService = new AdminUserServiceImpl();
        // 获取所有前端传来的数据，为null也没关系
        Long id = RequestUtil.parseLong(request, "id", null);
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        // 构建对象，方便后续使用
        AdminUser adminUser = new AdminUser();
        adminUser.setId(id);
        adminUser.setUsername(username);
        adminUser.setPassword(password);
        adminUser.setNickname(nickname);
        // 返回的json对象
        R result = null;
        if("page".equals(opr)){
            // 分页查询
            Integer pageNo = RequestUtil.parseInt(request, "pageNo", 1);
            Integer pageSize = RequestUtil.parseInt(request, "pageSize", 10);
            Page<AdminUser> page = adminUserService.findPage(adminUser, pageNo, pageSize);
            // 返回分页数据
            result = R.ok(page);
        }else if("list".equals(opr)){
            // 列表全部数据
            List<AdminUser> list = adminUserService.findAll();
            // 返回所有数据
            result = R.ok(list);
        }else if("save".equals(opr)){
            // 新增或修改数据，统一使用save
            boolean save = false;
            try {
                save = adminUserService.saveOrUpdate(adminUser);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            if(save){
                result = R.ok(adminUser);
            }else{
                result = R.fail(adminUser);
            }
        }else if("detail".equals(opr)){
            // 获取详情数据
            adminUser = adminUserService.findById(id);
            if(adminUser != null){
                result = R.ok(adminUser);
            }else{
                result = R.fail();
            }
        }else if("del".equals(opr)){
            // 删除单条数据
            boolean deleted = adminUserService.removeById(id);
            if(deleted){
                result = R.ok(id);
            }else{
                result = R.fail(id);
            }
        }else if("delBatch".equals(opr)){
            // 批量删除数据
            Long[] ids = RequestUtil.parseLongValues(request, "ids", null);
            boolean deleted = adminUserService.removeBatch(ids);
            if(deleted){
                result = R.ok(ids);
            }else{
                result = R.fail(ids);
            }
        }else if("importBatch".equals(opr)){
            // 批量导入数据
            boolean imported = false;
            try {
                List<AdminUser> adminUserList = RequestUtil.readImportFile(request, "importFile", AdminUser.class  , "username" , "password" , "nickname" );

                imported = adminUserService.importBatch(adminUserList.toArray(new AdminUser[adminUserList.size()]));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(imported){
                result = R.ok();
            }else{
                result = R.fail();
            }
        }else if("loginInfo".equals(opr)){
            AdminUser loginAdmin = (AdminUser) session.getAttribute(SysConstants.SESSION_LOGIN_ADMIN);
            // 返回之前去除密码
            loginAdmin.setPassword(null);
            if(loginAdmin != null){
                // 登录成功状态，则返回登录用户信息
                result = R.ok().data(loginAdmin);
            }else{
                result = R.fail();
            }
        }
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
